package ui;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

import dataconnect.ConnectionFactory;
import dataconnect.SqlUtil;
import org.eclipse.swt.custom.CCombo;

public class AnalyseInfoUI extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text txtSh;
	private Text txtQQIndsy;
	private Text txtSinaIndsy;
	private Text txtIndsyScore;
	private Text txtStockScore;
	private Text txtAverAmount;
	private Text txtPriceLevel;
	private Text txtAmountBat;
	private Text txtBatToDays;
	private Text txtMaxPrice;
	private Text txtMinPrice;
	private Text txtMaxToDays;
	private Text txtMinToDays;
	private Text txtWave;
	private Text txtLastPercent;
	private Text txtDownDays;
	private Text txtUpDays;
	private Text txtMessage;
	private CCombo comboInfo;
	private CCombo comboDealLevel;
	
	private Button ckIsOwn;
	
	private CLabel lblName;

	private String code;
	private CLabel label_18;
	private Text txtArea;
	private CLabel label_20;
	private Text txtIdealPrice;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public AnalyseInfoUI(Shell parent, int style,String code) {
		super(parent, style);
		setText("SWT Dialog");
		this.code = code;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		
		createContents();
		
		shell.setLocation(Display.getCurrent().getClientArea().width / 2 - shell.getShell().getSize().x/2, Display.getCurrent()
				.getClientArea().height / 2 - shell.getSize().y/2);
		
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.MAX);
		shell.setTouchEnabled(true);
		shell.setModified(true);
		shell.setSize(639, 408);
		shell.setText("\u80A1\u7968\u4FE1\u606F");
		
		Button btnSave = new Button(shell, SWT.NONE);
		btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int isOwn = ckIsOwn.getSelection()?1:0;
				String anlyzUdt = "update stockanlyz set score="+txtStockScore.getText()
						+",dealLevel="+comboDealLevel.getSelectionIndex()+",isown="+isOwn+",info="+comboInfo.getSelectionIndex()+",priceLevel="+txtPriceLevel.getText()
						+" where number='"+txtSh.getText()+"'";
				SqlUtil.update(anlyzUdt);
				MessageBox dlg = new MessageBox(shell);
				dlg.setMessage("保存成功！");
				dlg.open();
				shell.dispose();
			}
		});
		btnSave.setBounds(451, 341, 80, 25);
		btnSave.setText("\u4FDD\u5B58");
		
		Button btnCancel = new Button(shell, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnCancel.setBounds(536, 341, 80, 25);
		btnCancel.setText("\u53D6\u6D88");
		
		lblName = new CLabel(shell, SWT.NONE);
		lblName.setFont(SWTResourceManager.getFont("华文楷体", 13, SWT.BOLD));
		lblName.setBounds(236, 10, 80, 21);
		lblName.setText("\u4E0A\u8BC1\u6307\u6570\uFF1A");
		
		txtSh = new Text(shell, SWT.BORDER);
		txtSh.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				String stockStr = txtSh.getText();
				System.out.println(stockStr);
				String sql = "select count(number) from stockinfo where number like '%"+stockStr+"%' or name like '%"+stockStr+"%' or shortname like '%"+stockStr+"%'";
				Connection con = ConnectionFactory.getConnection();
				Statement stmt = null;
				ResultSet rs = null;
				ResultSet rs2 = null;
				try {
					stmt = con.createStatement();
					rs = stmt.executeQuery(sql);
					if(rs.next()){
						if(rs.getInt(1)==1){
							rs2 = stmt.executeQuery("select number from stockinfo where number like '%"+stockStr+"%' or name like '%"+stockStr+"%' or shortname like '%"+stockStr+"%'");
							if(rs2.next())
								loadData(rs2.getString(1));
						}
					}
				}catch (SQLException ex) {
					ex.printStackTrace();
				}finally{
					try {
						if(rs!=null){
							rs.close();
						}
						if(rs2!=null){
							rs2.close();
						}
						if(stmt!=null){
							stmt.close();
						}
					} catch (SQLException ex) {
						ex.printStackTrace();
					}
				}
			}
		});
		txtSh.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));
//		txtSh.setText("sh000001");
		txtSh.setBounds(322, 10, 80, 21);
		
		txtArea = new Text(shell, SWT.BORDER);
		txtArea.setEditable(false);
		txtArea.setBounds(543, 167, 73, 21);
		
		CLabel lblNewLabel_1 = new CLabel(shell, SWT.NONE);
		lblNewLabel_1.setBounds(10, 55, 60, 21);
		lblNewLabel_1.setText("\u817E\u8BAF\u884C\u4E1A\uFF1A");
		
		txtQQIndsy = new Text(shell, SWT.BORDER);
		txtQQIndsy.setEditable(false);
		txtQQIndsy.setBounds(76, 54, 73, 21);
		
		CLabel label = new CLabel(shell, SWT.NONE);
		label.setText("\u65B0\u6D6A\u884C\u4E1A\uFF1A");
		label.setBounds(10, 195, 60, 21);
		
		txtSinaIndsy = new Text(shell, SWT.BORDER);
		txtSinaIndsy.setEditable(false);
		txtSinaIndsy.setBounds(76, 194, 540, 21);
		
		CLabel label_1 = new CLabel(shell, SWT.NONE);
		label_1.setText("\u884C\u4E1A\u5F97\u5206\uFF1A");
		label_1.setBounds(10, 83, 60, 21);
		
		txtIndsyScore = new Text(shell, SWT.BORDER);
		txtIndsyScore.setBounds(76, 82, 73, 21);
		
		CLabel label_2 = new CLabel(shell, SWT.NONE);
		label_2.setText("\u80A1\u7968\u5F97\u5206\uFF1A");
		label_2.setBounds(10, 139, 60, 21);
		
		txtStockScore = new Text(shell, SWT.BORDER);
		txtStockScore.setBounds(76, 138, 73, 21);
		
		CLabel label_3 = new CLabel(shell, SWT.NONE);
		label_3.setText("\u64CD\u4F5C\u5907\u6CE8\uFF1A");
		label_3.setBounds(10, 167, 60, 21);
		
		CLabel label_4 = new CLabel(shell, SWT.NONE);
		label_4.setText("\u5747\u6210\u4EA4\u91CF\uFF1A");
		label_4.setBounds(166, 56, 60, 21);
		
		comboDealLevel = new CCombo(shell, SWT.BORDER);
		comboDealLevel.setItems(new String[] {" ", "\u5F3A\u52BF\u5173\u6CE8", "\u5F31\u52BF\u5173\u6CE8", "\u6A2A\u76D8\u5173\u6CE8", "\u4E0D\u5173\u6CE8"});
		comboDealLevel.setEditable(false);
		comboDealLevel.setBounds(76, 167, 73, 21);
		
		txtAverAmount = new Text(shell, SWT.BORDER);
		txtAverAmount.setEditable(false);
		txtAverAmount.setBounds(232, 55, 73, 21);
		
		CLabel label_5 = new CLabel(shell, SWT.NONE);
		label_5.setText("\u80A1\u7968\u8BC4\u7EA7\uFF1A");
		label_5.setBounds(166, 84, 60, 21);
		
		CLabel label_6 = new CLabel(shell, SWT.NONE);
		label_6.setText("\u80A1\u7968\u4EF7\u4F4D\uFF1A");
		label_6.setBounds(166, 112, 60, 21);
		
		txtPriceLevel = new Text(shell, SWT.BORDER);
		txtPriceLevel.setEditable(false);
		txtPriceLevel.setBounds(232, 111, 73, 21);
		
		CLabel label_7 = new CLabel(shell, SWT.NONE);
		label_7.setText("\u91CF\u500D\u7387\uFF1A");
		label_7.setBounds(166, 140, 60, 21);
		
		txtAmountBat = new Text(shell, SWT.BORDER);
		txtAmountBat.setEditable(false);
		txtAmountBat.setBounds(232, 139, 73, 21);
		
		CLabel label_8 = new CLabel(shell, SWT.NONE);
		label_8.setText("\u91CF\u589E\u5929\u6570\uFF1A");
		label_8.setBounds(166, 168, 60, 21);
		
		txtBatToDays = new Text(shell, SWT.BORDER);
		txtBatToDays.setEditable(false);
		txtBatToDays.setBounds(232, 167, 73, 21);
		
		txtMaxPrice = new Text(shell, SWT.BORDER);
		txtMaxPrice.setEditable(false);
		txtMaxPrice.setBounds(387, 82, 73, 21);
		
		CLabel label_10 = new CLabel(shell, SWT.NONE);
		label_10.setText("\u6700\u9AD8\u4EF7\u683C\uFF1A");
		label_10.setBounds(321, 83, 60, 21);
		
		ckIsOwn = new Button(shell, SWT.CHECK);
		ckIsOwn.setText("\u9009\u5165\u5173\u6CE8");
		ckIsOwn.setBounds(322, 55, 98, 16);
		
		CLabel label_11 = new CLabel(shell, SWT.NONE);
		label_11.setText("\u6700\u4F4E\u4EF7\u683C\uFF1A");
		label_11.setBounds(321, 111, 60, 21);
		
		comboInfo = new CCombo(shell, SWT.BORDER);
		comboInfo.setEditable(false);
		comboInfo.setItems(new String[] {" ", "\u5356\u51FA", "\u51CF\u6301", "\u4E2D\u6027", "\u589E\u6301", "\u4E70\u5165"});
		comboInfo.setBounds(232, 83, 73, 21);
		
		txtMinPrice = new Text(shell, SWT.BORDER);
		txtMinPrice.setEditable(false);
		txtMinPrice.setBounds(387, 110, 73, 21);
		
		txtMaxToDays = new Text(shell, SWT.BORDER);
		txtMaxToDays.setEditable(false);
		txtMaxToDays.setBounds(387, 138, 73, 21);
		
		CLabel label_12 = new CLabel(shell, SWT.NONE);
		label_12.setText("\u4EF7\u9AD8\u8DDD\u79BB\uFF1A");
		label_12.setBounds(321, 139, 60, 21);
		
		CLabel label_13 = new CLabel(shell, SWT.NONE);
		label_13.setText("\u4EF7\u4F4E\u8DDD\u79BB\uFF1A");
		label_13.setBounds(321, 167, 60, 21);
		
		txtMinToDays = new Text(shell, SWT.BORDER);
		txtMinToDays.setEditable(false);
		txtMinToDays.setBounds(387, 166, 73, 21);
		
		CLabel label_14 = new CLabel(shell, SWT.NONE);
		label_14.setText("\u6CE2\u52A8\u5E45\u5EA6\uFF1A");
		label_14.setBounds(477, 55, 60, 21);
		
		txtWave = new Text(shell, SWT.BORDER);
		txtWave.setBounds(543, 54, 73, 21);
		
		txtLastPercent = new Text(shell, SWT.BORDER | SWT.RIGHT);
		txtLastPercent.setEditable(false);
		txtLastPercent.setBounds(543, 82, 73, 21);
		
		CLabel label_15 = new CLabel(shell, SWT.NONE);
		label_15.setText("\u4E0A\u6B21\u6DA8\u5E45\uFF1A");
		label_15.setBounds(477, 83, 60, 21);
		
		CLabel label_16 = new CLabel(shell, SWT.NONE);
		label_16.setText("\u4E0B\u8DCC\u5929\u6570\uFF1A");
		label_16.setBounds(477, 140, 60, 21);
		
		txtDownDays = new Text(shell, SWT.BORDER);
		txtDownDays.setEditable(false);
		txtDownDays.setBounds(543, 139, 73, 21);
		
		txtUpDays = new Text(shell, SWT.BORDER);
		txtUpDays.setEditable(false);
		txtUpDays.setBounds(543, 110, 73, 21);
		
		CLabel label_17 = new CLabel(shell, SWT.NONE);
		label_17.setText("\u4E0A\u6DA8\u5929\u6570\uFF1A");
		label_17.setBounds(477, 111, 60, 21);
		
		label_18 = new CLabel(shell, SWT.NONE);
		label_18.setText("\u5730    \u533A\uFF1A");
		label_18.setBounds(477, 168, 60, 21);
		
		label_20 = new CLabel(shell, SWT.NONE);
		label_20.setText("\u4F30\u503C\u7A7A\u95F4\uFF1A");
		label_20.setBounds(10, 111, 60, 21);
		
		txtIdealPrice = new Text(shell, SWT.BORDER);
		txtIdealPrice.setBounds(76, 110, 73, 21);
		
		CLabel label_19 = new CLabel(shell, SWT.NONE);
		label_19.setText("\u80A1\u7968\u8BC4\u8BED\uFF1A");
		label_19.setBounds(10, 234, 60, 21);
		
		txtMessage = new Text(shell, SWT.BORDER);
		txtMessage.setBounds(76, 234, 540, 101);
		
		loadData(code);
	}
	
	private void loadData(String code){
		String sql = "select * from stockinfo join stockanlyz on stockinfo.number=stockanlyz.number where stockinfo.number='"+code+"'";
		Connection con = ConnectionFactory.getConnection();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				txtSh.setText(code);
				lblName.setText(rs.getString("name"));
				if(rs.getString("industry")!=null){
					txtQQIndsy.setText(rs.getString("industry"));
				}
				txtSinaIndsy.setText(rs.getString("sinaIndustry"));
//				txtDealMark.setText(rs.getString("dealmark"));
				txtMessage.setText(rs.getString("message"));
				txtArea.setText(rs.getString("area"));
				
				txtAverAmount.setText(rs.getBigDecimal("averAmount").divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP).toString()+"万");
				txtAmountBat.setText(rs.getBigDecimal("amountBat").toString());
				txtLastPercent.setText(rs.getBigDecimal("ratePercent").toString());
				txtMaxPrice.setText(rs.getBigDecimal("maxPice").toString());
				txtMinPrice.setText(rs.getBigDecimal("minPrice").toString());
				txtWave.setText(rs.getBigDecimal("dayPercentWave").toString());
				txtIdealPrice.setText(rs.getBigDecimal("idealPrice").toString());
				txtStockScore.setText(rs.getBigDecimal("score").toString());
				
				txtBatToDays.setText(String.valueOf(rs.getInt("amtCreDays")));
				txtDownDays.setText(String.valueOf(rs.getInt("fallDays")));
				txtUpDays.setText(String.valueOf(rs.getInt("upDays")));
//				txtIndsyScore.setText(String.valueOf(rs.getInt("")));
				txtMaxToDays.setText(String.valueOf(rs.getInt("maxToDays")));
				txtMinToDays.setText(String.valueOf(rs.getInt("minToDays")));
				txtPriceLevel.setText(String.valueOf(rs.getInt("priceLevel")));
				
				comboInfo.select(rs.getInt("info"));
				comboDealLevel.select(rs.getInt("dealLevel"));
				ckIsOwn.setSelection(rs.getInt("isOwn")==1);
//				txtIsOwn.setText(String.valueOf(rs.getInt("isOwn")));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
}
